package com.zzhrtech.wgzx_cityzen.ui.contact;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.afollestad.materialdialogs.MaterialDialog;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.contact.PrizeListAdapter;
import com.zzhrtech.wgzx_cityzen.beans.contact.JifenDhBean;
import com.zzhrtech.wgzx_cityzen.beans.contact.PrizeListBean;
import com.zzhrtech.wgzx_cityzen.view.CustomProgress;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PrizeListActivity extends BaseActivity {

    RecyclerView recy_zjjl;

    private CustomProgress proDialog;
    private PrizeListAdapter prizeListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prize_list);

        toolbar.setNavigationIcon(R.mipmap.ic_close);

        recy_zjjl = (RecyclerView) findViewById(R.id.recy_zjjl);
        recy_zjjl.setLayoutManager(new LinearLayoutManager(this));
        recy_zjjl.setItemAnimator(new DefaultItemAnimator());


        prizeListAdapter = new PrizeListAdapter();
        recy_zjjl.setAdapter(prizeListAdapter);


        proDialog = CustomProgress.show(this,true,null);
        proDialog.show();

        httpGetGoods();
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {

                proDialog.dismiss();

                List<PrizeListBean.Data> datas = (List<PrizeListBean.Data>) msg.obj;

                for (PrizeListBean.Data data : datas){
                    prizeListAdapter.addItem(data);
                }

            }

            return false;
        }
    });


    private void httpGetGoods(){

//        progress = new MaterialDialog.Builder(this)
//                .title("请等待")
//                .content("加载中")
//                .progress(true,0)
//                .show();


        Map<String,String> map = new HashMap<String, String>();
        map.put("citizen_id",getCache().getAsString(keyUid));

        GsonRequest<PrizeListBean> gsonRequest = new GsonRequest<PrizeListBean>(Request.Method.POST, Constans.HTTP_PRIZELIST, PrizeListBean.class, map, new com.android.volley.Response.Listener<PrizeListBean>() {
            @Override
            public void onResponse(PrizeListBean response) {

                if (response.isSuccess()) {
                    Message message = new Message();
                    message.what = 0;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                }else {
                    proDialog.dismiss();
                    showToast(response.getMsg());
                }

            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                proDialog.dismiss();

                Log.d("result=====",error + "");
//                dialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }
}
